package user.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Dialog.ModalExclusionType;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import user.dao.BookAdminDao;
import user.model.Book;

import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.UIManager;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BookManagerFrame extends JFrame {

	private JPanel contentPane;
	private JTextField bookName;
	private JTextField author;
	private JTable table;
	private JTextField bookidTxt;
	private JTextField bookNameTxt;
	private JTextField authorTxt;
	private JTextField priceTxt;
	private JTextField typeTxt;
	private BookAdminDao bookAdminDao = new BookAdminDao();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookManagerFrame frame = new BookManagerFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookManagerFrame() {
		setTitle("图书维护");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 758, 761);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel label1 = new JLabel("图书名称");
		label1.setFont(new Font("宋体", Font.PLAIN, 18));
		
		bookName = new JTextField();
		bookName.setColumns(10);
		
		JLabel label6 = new JLabel("作者");
		label6.setFont(new Font("宋体", Font.PLAIN, 18));
		
		author = new JTextField();
		author.setColumns(10);
		
		JButton button1 = new JButton("查找");
		button1.setIcon(new ImageIcon(BookManagerFrame.class.getResource("/images/查询.png")));
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SelectBookActionPerformed(e);
			}
		});
		button1.setFont(new Font("宋体", Font.PLAIN, 18));
		
		JScrollPane scrollPane = new JScrollPane();
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
						BookMousePressed(e);
			}
		});
		table.setBackground(Color.WHITE);
		table.setForeground(Color.BLACK);
		table.setFont(new Font("宋体", Font.PLAIN, 15));
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u56FE\u4E66\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u4F5C\u8005", "\u56FE\u4E66\u7C7B\u522B", "\u56FE\u4E66\u4EF7\u683C", "\u662F\u5426\u5728\u9986"
			}
		));
		table.getColumnModel().getColumn(1).setPreferredWidth(91);
		table.getColumnModel().getColumn(1).setMinWidth(30);
		scrollPane.setViewportView(table);
		JLabel label2 = new JLabel("图书编号");
		label2.setFont(new Font("宋体", Font.PLAIN, 18));
		
		bookidTxt = new JTextField();
		bookidTxt.setEditable(false);
		bookidTxt.setFont(new Font("宋体", Font.PLAIN, 18));
		bookidTxt.setColumns(10);
		
		JLabel label4 = new JLabel("图书名称");
		label4.setFont(new Font("宋体", Font.PLAIN, 18));
		
		bookNameTxt = new JTextField();
		bookNameTxt.setFont(new Font("宋体", Font.PLAIN, 18));
		bookNameTxt.setColumns(10);
		
		JLabel label3 = new JLabel("图书作者");
		label3.setFont(new Font("宋体", Font.PLAIN, 18));
		
		authorTxt = new JTextField();
		authorTxt.setFont(new Font("宋体", Font.PLAIN, 18));
		authorTxt.setColumns(10);
		
		JLabel label5 = new JLabel("图书价格");
		label5.setFont(new Font("宋体", Font.PLAIN, 18));
		
		priceTxt = new JTextField();
		priceTxt.setFont(new Font("宋体", Font.PLAIN, 18));
		priceTxt.setColumns(10);
		
		JButton button4 = new JButton("图书添加");
		button4.setIcon(new ImageIcon(BookManagerFrame.class.getResource("/images/增加.png")));
		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new BookAddDialog().setVisible(true);
				
			}
		});
		button4.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JButton button2 = new JButton("修改");
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					UpdateBookActionPerformed(e);
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, "请选择一行！");
				}
				
			}
		});
		button2.setIcon(new ImageIcon(BookManagerFrame.class.getResource("/images/修改.png")));
		button2.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JButton button3 = new JButton("返回");
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new BookAdminFrame().setVisible(true);
				dispose();
			}
		});
		button3.setIcon(new ImageIcon(BookManagerFrame.class.getResource("/images/退出 (1).png")));
		button3.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JButton btnNewButton_4 = new JButton("重置");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		btnNewButton_4.setIcon(new ImageIcon(BookManagerFrame.class.getResource("/images/重置.png")));
		btnNewButton_4.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JLabel label7 = new JLabel("图书类别");
		label7.setFont(new Font("宋体", Font.PLAIN, 18));
		
		typeTxt = new JTextField();
		typeTxt.setFont(new Font("宋体", Font.PLAIN, 18));
		typeTxt.setColumns(10);
		
		JButton button = new JButton("删除");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					DeleteBookActionPerformed(e);
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, "请选择一行！");
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(35)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(label1)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(bookName, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
									.addGap(50)
									.addComponent(label6)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(author, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
									.addComponent(button1)))
							.addGap(97))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
											.addGroup(gl_contentPane.createSequentialGroup()
												.addComponent(label2)
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addComponent(bookidTxt, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))
											.addGroup(gl_contentPane.createSequentialGroup()
												.addComponent(label3)
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
													.addComponent(typeTxt, GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
													.addComponent(authorTxt))))
										.addComponent(label7))
									.addGap(70)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(label4)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 167, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(label5)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(24)
									.addComponent(btnNewButton_4)
									.addGap(64)
									.addComponent(button2)
									.addGap(59)
									.addComponent(button, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(button3)
								.addComponent(button4, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
							.addGap(21))))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(36)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label1)
						.addComponent(button1)
						.addComponent(author, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label6))
					.addGap(20)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 340, GroupLayout.PREFERRED_SIZE)
					.addGap(41)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(label4)
								.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(label5)
								.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(label2)
								.addComponent(bookidTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label3))))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(36)
							.addComponent(button4, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(button3, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(label7)
								.addComponent(typeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnNewButton_4, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
								.addComponent(button2, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
								.addComponent(button, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
							.addGap(45))))
		);
		
		
		contentPane.setLayout(gl_contentPane);
		this.setLocationRelativeTo(null);
		fillTable("", "");
	}
	
	protected void DeleteBookActionPerformed(ActionEvent e) {
		String id = this.bookidTxt.getText();
		int bookid = Integer.parseInt(id);
		try {
			int i = bookAdminDao.deleteBook(bookid);
			if(i>0){
				JOptionPane.showMessageDialog(null, "删除成功");
				reset();
				fillTable("", "");
			}else {
				JOptionPane.showMessageDialog(null, "删除失败");
				}
		} catch (Exception e1) {
			e1.printStackTrace();
			
		}
	}

	protected void UpdateBookActionPerformed(ActionEvent e) {
		String id = this.bookidTxt.getText();
		String bookName = this.bookNameTxt.getText();
		String author = this.authorTxt.getText();
		String type = this.typeTxt.getText();
		String price = this.priceTxt.getText();
		Book book = new Book(Integer.parseInt(id),bookName,author,type,Float.parseFloat(price));
		try {
			int i = bookAdminDao.updateBook(book);
			if(i>0) {
				JOptionPane.showMessageDialog(null, "修改成功");
				reset();
				fillTable("", "");
			}else {
				JOptionPane.showMessageDialog(null, "修改失败");
				}
			} catch (Exception e1) {
			e1.printStackTrace();
			}
		
	}

	public void reset() {
		bookidTxt.setText("");
		bookNameTxt.setText("");
		authorTxt.setText("");
		typeTxt.setText("");
		priceTxt.setText("");
	}

	public void fillTable(String bookName,String author) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
	    dtm.setRowCount(0);//设置成0行
	    //查询出书籍
	   
	    List<Book> bookList = bookAdminDao.listBook(bookName, author);
	    for(Book book:bookList) {
	    	Vector v = new Vector<>();
	    	v.add(book.getBookId());
	    	v.add(book.getBookName());
	    	v.add(book.getAuthor());
	    	v.add(book.getType());
	    	v.add(book.getPrice());
	    	if(Integer.valueOf(book.getLend()).equals(0)) {
	    		v.add("否");
			}
			else {
				v.add("是");
			}
	    	dtm.addRow(v);
	    }
	}
	protected void SelectBookActionPerformed(ActionEvent e) {
		String bookName = this.bookName.getText();
		String author = this.author.getText();
		fillTable(bookName, author);
	}
	protected void BookMousePressed(MouseEvent e) {
		int row = table.getSelectedRow();//获得行号
		bookidTxt.setText(String.valueOf(table.getValueAt(row, 0)));//获取第一列信息
		bookNameTxt.setText((String) table.getValueAt(row, 1));
		authorTxt.setText((String) table.getValueAt(row, 2));
		typeTxt.setText(String.valueOf(table.getValueAt(row, 3)));
		priceTxt.setText(String.valueOf(table.getValueAt(row, 4)));
		}
}
